<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html xmlns:th="" lang="zh-CN">
<%@include file="/WEB-INF/pages/base/include.jsp"%>
<body>
<div id="wrapper">
    <%@include file="/WEB-INF/pages/base/menu.jsp"%>
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h3 class="page-header">订单管理</h3>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="panel panel-default">
                    <div class="panel-heading">订单列表</div>
                    <div class="panel-body inner-body">
                        <div class="main-box-search">
                            <form method="get" name="from1"  id="searchForm"  >
                                <div class="search-row">
                                    <input type="hidden" name="" value="">
                                    <div class="col-lg-3 col-md-6" style=" margin-bottom:15px; padding-left:0px;">
                                        <input class="form-control" type="text" id="orderId" placeholder="请输入订单id">
                                    </div>
                                    <div class="col-lg-3 col-md-6" style=" margin-bottom:15px; padding-left:0px;">
                                        <input class="form-control" type="text" id="accountId" placeholder="请输入购买者id">
                                    </div>
                                     <div class="col-lg-2 col-md-6" style="margin-bottom:15px; padding-left:0px;">
                                        <select class="form-control" id="status">
                                            <option value="0">全部订单</option>
                                            <option value="1">未付款</option>
                                            <option value="2">待处理</option>
                                            <option value="3">已处理</option>
                                            <option value="4">历史订单</option>
                                        </select>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div class="dataTable_wrapper">
                            <table class="table table-striped table-bordered table-hover" id="ta">
                                <thead>
                                <tr>
                                    <th class="text-center" style="width:13%;">订单编号</th>
                                    <th class="text-center" style="width:7%;">供应商</th>
                                    <th class="text-center" style="width:7%;">订单商品类型</th>
                                    <th class="text-center" style="width:10%;">购买者id</th>
                                    <th class="text-center" style="width:10%;">购买者昵称</th>
                                    <th class="text-center" style="width:8%;">实付金额</th>
                                    <th class="text-center" style="width:8%;">订单状态</th>
                                    <th class="text-center" style="width:22%">订单时间</th>
                                    <th class="text-center" style="width:8%;">操作</th>
                                </tr>
                                </thead>
                                <tbody  id="orderList">
                                
                                </tbody>
                            </table>
                        </div>
                        <div class="modal fade" id="sendModel">
                            <div class="modal-dialog">
                                <div class="modal-content message_align">
                                    <div class="modal-header">
                                        <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">×</span>
                                        </button>
                                        <h4 class="modal-title">物流信息</h4>
                                    </div>
                                    <div class="modal-body">
                                        <div class="form-group">
                                            <label class="col-lg-3 clo-md-3 text-right"
                                                   style="line-height:34px;">物流渠道：</label>
                                            <div class="col-lg-6 col-md-6">
                                                <select class="form-control" id="logisticsList">
                                                		<c:forEach var="item" items="${logistics}" varStatus="status">
									                             <option value="${item.code}">${item.name}</option>
									                    </c:forEach>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="clearfix"></div>
                                        <div class="form-group">
                                            <label class="col-lg-3 clo-md-3 text-right"
                                                   style="line-height:34px;">物流单号：</label>
                                            <div class="col-lg-6 col-md-6">
                                                <input class="form-control" id="logisticsNumber" type="text" placeholder="物流单号" onkeyup="value=value.replace(/[^\w]/ig,'')">
                                            </div>
                                        </div>                                        
                                        <div class="clearfix"></div>
                                        <div class="form-group" style="margin-bottom:30px;">
                                            <label class="col-lg-3 clo-md-3 text-right"
                                                   style="line-height:34px;">物流费用：</label>
                                            <div class="col-lg-6 col-md-6">
                                                <input class="form-control" id="money" type="text" value="0"
                                                       placeholder="物流费用">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <input id="orderId_hidden" type="hidden">
                                        <button class="btn btn-default" type="button" data-dismiss="modal">取消</button>
                                        <a class="btn btn-danger" onclick="deliver_goods()" data-dismiss="modal">发货</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- <div class="text-right">
                            <ul class="pagination" id="pagination">
                                <li><a href="javascript:void(0);" onclick="" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
				                <li><a  href="javascript:void(0);" onclick="" >1</a></li>
				                <li><a href="javascript:void(0);" onclick=""  aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
                            </ul>
                        </div> -->
                        <div class="box">
							<div id="pagination3" class="page fl"></div>
						</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
	var ref = "";
	searchCase(0,1);
	/* ref = setInterval(function(){
		searchCase(0,1);
		},60000); */
	var excelPage = 1;
	$('#searchForm').change(function(e) {
		searchCase(1,1);
	})
	
	//搜索
	function searchCase(flag,page) {
		excelPage = page;
		var status = null;
		var mallUuid = "${searchMallUuid}";
		if(flag == 1){
			status=$("#status option:selected").val();  //获取选中的项
			if(status == 0){
				status = null;
			}
		}
		var enabled = "${enabled}";
		if(enabled == 0){
			mallUuid = "${mallUuid}";
		}
		var orderId = $("#orderId").val();
		var accountId = $("#accountId").val();
		
		var pageSize= 10;
		
		var baseURL = "${webPath}/order/list";
	    var json_package = {
	        "head": {},
	        "body": {
	        	"orderId": orderId,
	        	"accountId": accountId,
	        	"status": status,
	        	"mallUuid":mallUuid,
	            "page": page,
	            "pageSize": pageSize
	        }
	    }
	    $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
	        if (typeof result == 'string') result = JSON.parse(result);
	        if (result.body.result == '1') {
	            alert(result.body.description)
	            $("#orderList").empty();
	        } else {
	        	$("#orderList").empty();
	        	var list = result.body.list;
	        	var count = result.body.count;
	        	var str = "";
	        	if(list.length<=0){
	        		return
	        	}
	        	
	        	for(var i=0;i<list.length;i++){
	        		str += '<tr>';
	        		str += '	<td class="text-center"><a href="${webPath}toOrderDetail/'+list[i].id+'" target="_blank" >'+list[i].id+'</a></td>';
	        		str += '	<td class="text-center">'+list[i].sellerName+'</td>';
	        		str += '	<td class="text-center">'+list[i].productType+'</td>';
	        		str += '	<td class="text-center">'+list[i].accountId+'</td>';
	        		str += '	<td class="text-center">'+list[i].accountNickName+'</td>';
	        		str += '	<td class="text-center">'+list[i].realPrice+'</td>';
	        		str += '	<td class="text-center">'+list[i].status+'</td>';
	        		str += '	<td>下单时间：'+list[i].createTime+'<br>付款时间：'+list[i].payTime+'<br>发货时间：'+list[i].sendTime+'<br></td>';
					str += '	<td class="text-center">';
					if("已支付,待发货" == list[i].status){
						str += '		<button class="btn btn-default  deliver_goods" onclick=send('+list[i].id+')>发货</button>';
					}
					str += '	</td>';
					str += '</tr>';
	        	}
	        	$("#orderList").append(str);
	        	
	        	
	        	var currentPage = page;
	        	var sumPage = Math.ceil(count/pageSize);
	        	
	        	$("#pagination3").pagination({
					currentPage: currentPage,
					totalPage: sumPage,
					isShow: true,
					count: 10,
					homePageText: "首页",
					endPageText: "尾页",
					prevPageText: "上一页",
					nextPageText: "下一页",
					callback: function(current) {
						searchCase(1,current);
					}
				});
	        }
	    })
	}
	
	function getLogisticsList() {
		var baseURL = "${webPath}/order/logisticsList";
	    var json_package = {
	        "head": {},
	        "body": {
	        }
	    }
	    $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
	        if (typeof result == 'string') result = JSON.parse(result);
	        console.log(result);
	        if (result.body.result == '1') {
	            alert(result.body.description)
	            $("#logisticsList").empty();
	        } else {
	        	$("#logisticsList").empty();
	        	var list = result.body.logisticsList;
	        	var str = "";
	        	if(list.length<=0){
	        		return
	        	}
	        	for(var i=0;i<list.length;i++){
	        		str += '<option value="'+list[i].code+'">'+list[i].name+'</option>';
	        	}
	        	$("#logisticsList").append(str);
	        }
	    })
	}
	
    function send(orderId) {
        $('#sendModel').modal();
        $("#orderId_hidden").val(orderId);
    }

    
    var deliver_goods = function () {
        var baseURL = "${webPath}/order/sendOrder";
        var orderId = $("#orderId_hidden").val();
        var logisticsCode = $('#logisticsList option:selected').val();
        var logisticsNumber = $("#logisticsNumber").val();
        if(logisticsNumber.length<8){
        	alert("请输入正常单号,现单号长度为"+logisticsNumber.length);
        	return;
        }
        if(logisticsNumber.length>13){
        	alert("请输入正常单号,现单号长度为"+logisticsNumber.length);
        	return;
        }
        var json_package = {
            "head": {},
            "body": {"orderId": orderId, "logisticsCode": logisticsCode, "logisticsNumber": logisticsNumber}
        };
        $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
            if (typeof result == 'string') result = JSON.parse(result);
            if (result.body.result == '0') {
                alert('发货成功');
                searchCase(1,excelPage);
            } else {
                alert(result.body.description)
            }
        })
    };
    var idTmr;
    function  getExplorer() {
        var explorer = window.navigator.userAgent ;
        //ie
        if (explorer.indexOf("MSIE") >= 0) {
            return 'ie';
        }
        //firefox
        else if (explorer.indexOf("Firefox") >= 0) {
            return 'Firefox';
        }
        //Chrome
        else if(explorer.indexOf("Chrome") >= 0){
            return 'Chrome';
        }
        //Opera
        else if(explorer.indexOf("Opera") >= 0){
            return 'Opera';
        }
        //Safari
        else if(explorer.indexOf("Safari") >= 0){
            return 'Safari';
        }
    }
    function method1(tableid) {//整个表格拷贝到EXCEL中
        if(getExplorer()=='ie')
        {
            var curTbl = document.getElementById(tableid);
            var oXL = new ActiveXObject("Excel.Application");

            //创建AX对象excel
            var oWB = oXL.Workbooks.Add();
            //获取workbook对象
            var xlsheet = oWB.Worksheets(1);
            //激活当前sheet
            var sel = document.body.createTextRange();
            sel.moveToElementText(curTbl);
            //把表格中的内容移到TextRange中
            sel.select;
            //全选TextRange中内容
            sel.execCommand("Copy");
            //复制TextRange中内容
            xlsheet.Paste();
            //粘贴到活动的EXCEL中
            oXL.Visible = true;
            //设置excel可见属性

            try {
                var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
            } catch (e) {
                print("Nested catch caught " + e);
            } finally {
                oWB.SaveAs(fname);

                oWB.Close(savechanges = false);
                //xls.visible = false;
                oXL.Quit();
                oXL = null;
                //结束excel进程，退出完成
                //window.setInterval("Cleanup();",1);
                idTmr = window.setInterval("Cleanup();", 1);

            }

        }
        else
        {
            tableToExcel('ta')
        }
    }
    function Cleanup() {
        window.clearInterval(idTmr);
        CollectGarbage();
    }
    var tableToExcel = (function() {
        var uri = 'data:application/vnd.ms-excel;base64,',
            template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
            base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) },
            format = function(s, c) {
                return s.replace(/{(\w+)}/g,
                    function(m, p) { return c[p]; }) }
        return function(table, name) {
            if (!table.nodeType) table = document.getElementById(table)
            var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
            window.location.href = uri + base64(format(template, ctx))
        }
    })()
</script>
</body>
</html>
